package com.klikmee.sms.core.base.communicator.telenor;

import javax.persistence.NoResultException;

import com.klikmee.sms.core.base.communicator.AbstractDeliveryReport2MessageResolver;
import com.klikmee.sms.core.base.communicator.DeliveryReport;
import com.klikmee.sms.core.base.exceptions.DeliveryReportNotResolvedException;
import com.klikmee.sms.core.commons.exceptions.ErrorInfo;
import com.klikmee.sms.core.model.OutgoingMessage;
/**
 * <p>
 * Delivery report resolver for Telenor communicator.
 * Resolves message by id and provider.
 * </p>
 * @author makcro
 *
 */
public class TelenorDeliveryReport2MessageResolverImpl extends AbstractDeliveryReport2MessageResolver {

	/**
	 * Finds sent message by id and provider.
	 * @param deliveryReport holder for delivery report info
	 * @return sent message for this delivery report
	 * @throws DeliveryReportNotResolvedException in case when message could not be resolved
	 */
	@Override
	public OutgoingMessage resolve(DeliveryReport deliveryReport) throws DeliveryReportNotResolvedException {
		try {
			return this.outgoingMessageDAO.findByMessageIdAndProvider(deliveryReport.getMessageId(), deliveryReport.getProvider());
		} catch (NoResultException e) {
			DeliveryReportNotResolvedException exception = new DeliveryReportNotResolvedException("Could not resolve delivery report for telenor provider");
			ErrorInfo errorInfo = exception.addInfo();
			errorInfo.getParameters().put("MessageId", deliveryReport.getMessageId());
			
			throw exception;
		}

	}

}
